using Eno.Application.Dto;
using Eno.Application.Services;
using Eno.Infrastructure.Tools;
using Microsoft.AspNetCore.Mvc;

namespace Eno.Api.Controllers;

[ApiController]
[Route("api/[controller]")]

public class PermissionRoleController : ControllerBase
{
    private readonly AppointPermissionService _service;

    public PermissionRoleController(AppointPermissionService appointPermissionService)
    {
        _service = appointPermissionService;
    }

    [HttpGet("{roleId}")]
    public async Task<IActionResult> GetRolePermissions(Guid roleId)
    {
        try
        {
            var permissions = await _service.GetRolePermissionsAsync(roleId);
            return Ok(Tips<IEnumerable<object>>.Success(permissions, "获取角色权限成功"));
        }
        catch (Exception)
        {
            return Ok(Tips<IEnumerable<object>>.Fail(Enumerable.Empty<object>()));
        }
    }

    [HttpPost]
    public async Task<IActionResult> AppointPermission(PermissionRoleDto permissionRoleDto)
    {
        var number = await _service.AppointPermissionToRole(permissionRoleDto.RoleId, permissionRoleDto.PermissionId);
        if (number == 0) return Ok(Tips<string>.Fail(""));
        return Ok(Tips<int>.Success(number, "为角色添加权限成功"));

    }

    [HttpDelete("{rId}")]
    public async Task<IActionResult> RemovePermission(Guid rId)
    {
        var number = await _service.RemovePermissionInRole(rId);
        if (number == 0) return Ok(Tips<string>.Fail(""));
        return Ok(Tips<int>.Success(number, "移除角色权限成功"));
    }

    [HttpDelete]
    public async Task<IActionResult> RemoveRolePermission([FromQuery] Guid roleId, [FromQuery] Guid permissionId)
    {
        var number = await _service.RemoveSpecificPermissionInRole(roleId, permissionId);
        if (number == 0) return Ok(Tips<string>.Fail(""));
        return Ok(Tips<int>.Success(number, "移除角色的指定权限成功"));
    }
}